/**
 * 可读性高版//
 */
class Solution {
    public int longestOnes(int[] nums, int k) {
        int left = 0;
        int right = 0;
        int maxLen = 0;
        int zero = 0;
        for (; right < nums.length; right++) {
            if (nums[right] == 0) {
                if (zero <= k) {
                    zero++;
                }
                while (zero > k) {
                    if (nums[left] != 0) {
                        left++;
                    } else {
                        zero--;
                        left++;
                    }
                }
            }
            maxLen = Math.max(right - left + 1, maxLen);
        }
        return maxLen;
    }
}

/**
 * 简化版
 */
class Solution1 {
    public int longestOnes(int[] nums, int k) {
        int left = 0;
        int right = 0;
        int maxLen = 0;
        int zero = 0;
        for (; right < nums.length; right++) {
            if (nums[right] == 0)
                zero++;
            while (zero > k)
                if (nums[left++] == 0)
                    zero--;
            maxLen = Math.max(right - left + 1, maxLen);
        }
        return maxLen;
    }
}